﻿// -----------------------------------------------------------------------
// <copyright file="DmQuocGiaDataAccess.cs" company="">
// TODO: Copyright by dangppsoft@gmail.com.
// </copyright>
// -----------------------------------------------------------------------
namespace ManagerClinic.Core.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ManagerClinic.Core.Common;
    using System.Data.Common;
    using System.Data;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using Microsoft.Practices.EnterpriseLibrary.Common;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using ManagerClinic.Ultilities.Ultility;

    public class DmQuocGiaDataAccess
    {
        public bool InsertDmQuocGia(DmQuocGiaEntity obj)
        {
            try
            {
                string sql = "spDmQuocGia_Insert";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaQuocGia", DbType.String, obj.MaQuocGia);
                db.AddInParameter(cmd, "TenQuocGia", DbType.String, obj.TenQuocGia);
                db.AddInParameter(cmd, "MoTa", DbType.String, obj.MoTa);
                db.AddInParameter(cmd, "TrangThaiSuDung", DbType.Boolean, obj.TrangThaiSuDung);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("InsertDmQuocGia", ex.Message); throw ex; }
        }

        public bool EditDmQuocGia(DmQuocGiaEntity obj)
        {
            try
            {
                string sql = "spDmQuocGia_Update";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaQuocGia", DbType.String, obj.MaQuocGia);
                db.AddInParameter(cmd, "TenQuocGia", DbType.String, obj.TenQuocGia);
                db.AddInParameter(cmd, "MoTa", DbType.String, obj.MoTa);
                db.AddInParameter(cmd, "TrangThaiSuDung", DbType.Boolean, obj.TrangThaiSuDung);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("UpdateDmQuocGia", ex.Message); throw ex; }
        }

        public bool DeleteDmQuocGia(string maQuocGia)
        {
            try
            {
                string sql = "spDmQuocGia_DeleteByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaQuocGia", DbType.String, maQuocGia);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("DeleteDmQuocGia", ex.Message); throw ex; }
        }

        public DmQuocGiaEntity GetDmQuocGia(string maQuocGia)
        {
            try
            {
                DmQuocGiaEntity obj = null;
                string sql = "spDmQuocGia_SelectByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaQuocGia", DbType.String, maQuocGia);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new DmQuocGiaEntity();
                        obj.MaQuocGia = reader["MaQuocGia"].ToString();
                        obj.TenQuocGia = reader["TenQuocGia"].ToString();
                        obj.MoTa = reader["MoTa"].ToString();
                        obj.TrangThaiSuDung = String.IsNullOrEmpty(reader["TrangThaiSuDung"].ToString()) == true ? false : (bool)reader["TrangThaiSuDung"];
                    }
                }
                return obj;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetDmQuocGia", ex.Message); throw ex; }
        }

        public List<DmQuocGiaEntity> GetDmQuocGiaList()
        {
            try
            {
                List<DmQuocGiaEntity> list = new List<DmQuocGiaEntity>();
                DmQuocGiaEntity obj = null;
                string sql = "spDmQuocGia_SelectAll";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new DmQuocGiaEntity();
                        obj.MaQuocGia = reader["MaQuocGia"].ToString();
                        obj.TenQuocGia = reader["TenQuocGia"].ToString();
                        obj.MoTa = reader["MoTa"].ToString();
                        obj.TrangThaiSuDung = String.IsNullOrEmpty(reader["TrangThaiSuDung"].ToString()) == true ? false : (bool)reader["TrangThaiSuDung"];
                        list.Add(obj);
                    }
                }
                return list;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetDmQuocGiaList", ex.Message); throw ex; }
        }

    }
}